package com.miaoxie.forum.module.monitor.shared.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by miaoxie on 2017/2/21.
 */
 public  class DateUtil {
    /**
     * 时间每五分钟取整
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getFiveMin(String date) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        Date d = sdf.parse(date);
        int min = d.getMinutes();
        if(min%5==0){
//            d.setMinutes(min-5);
        }else {
            d.setMinutes(min / 5 * 5);
        }
        return d;
    }

    /**
     * 获取月的时间集合
     * @param date 传入日期
     * @param daynum    往前推几个月
     * @param num   要多少个时间
     * @return
     */
    public static List<Date> getMonths(Date date,int daynum,int num){
        List<Date> dateList= new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, daynum);
        dateList.add(cal.getTime());
        for(int i=0;i<num-1;i++){
             cal = Calendar.getInstance();
            cal.setTime(dateList.get(i));
            cal.add(Calendar.MONTH, daynum);
            dateList.add(cal.getTime());
        }

        return dateList;
    }
    /**
     * 获取月的时间集合
     * @param date 传入日期
     * @param daynum    往前推几个月
     * @param num   要多少个时间
     * @return
     */
    public static List<Date> getDays(Date date,int daynum,int num){
        List<Date> dateList= new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        dateList.add(cal.getTime());
        for(int i=0;i<num-2;i++){
            cal = Calendar.getInstance();
            cal.setTime(dateList.get(i));
            cal.add(Calendar.DAY_OF_MONTH, daynum);
            dateList.add(cal.getTime());
        }

        return dateList;
    }
    /**
     * 往前推day个月
     * @param date
     * @param day
     * @return
     */
    public static Date getMonthByDay(Date date,int day){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, day);
        return cal.getTime();
    }

    /**
     * 往前推day天
     * @param date
     * @param day
     * @return
     */
    public static Date getDayByNum(Date date,int day){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 往前推day分钟
     * @param date
     * @param day
     * @return
     */
    public static Date getMimByNum(Date date,int day){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, day);
        return cal.getTime();
    }
    public static Date getFiveMin(Date date) throws ParseException {

        int min = date.getMinutes();
        if(min%5==0){
//            date.setMinutes(min-5);
        }else {
            date.setMinutes(min / 5 * 5);
        }
        return date;
    }

    /**
     * 获取分钟的时间集合
     * @param date
     * @param num 要几个
     * @param ms    两个时间相差多少
     * @return
     */
    public static List<Date> getMins(Date date,int num,int ms){
        List<Date> dates = new ArrayList<>();
        dates.add(date);
        for (int i = 0;i<num;i++){
           Date d =  new Date(dates.get(i).getTime() +ms);
            dates.add(d);
            d = null;
        }
        return dates;

    }

    /**
     * 按毫秒数处理时间
     * @param date
     * @param ms
     * @return
     */
    public static Date getDateByms(Date date,int ms){
        date= new Date(date.getTime() +ms);
        return date;

    }

    /**
     * 获取当日0:00
     * @param date
     * @return
     */
    public static Date getDayStartTime(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        return cal.getTime();
    }

    /**
     * 获取当月1号0：00
     * @param date
     * @return
     */
    public static Date getMonthStartTime(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取当年1月1号0:00
     * @param date
     * @return
     */
    public static Date getYearStartTime(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        return cal.getTime();
    }

    /**
     * 获取当前日期是今年的多少天
     * @param date
     * @return
     * @throws ParseException
     */
    public static Integer getDayNum(String date) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(date);
        Calendar currCal=Calendar.getInstance();
        currCal.setTime(d);
        currCal.set(Calendar.HOUR_OF_DAY, 0);
        Calendar currCal2=Calendar.getInstance();
        currCal2.set(Calendar.MONTH,0);
        currCal2.set(Calendar.HOUR_OF_DAY, 0);
        int currentYear = currCal2.get(Calendar.YEAR);
        currCal2.setTime(getYearFirst(currentYear));
        long a = currCal.getTimeInMillis()-currCal2.getTimeInMillis();
        a=a/(1000*3600*24);
        return (int)a;
    }

    /**
     * 获取当前日期是当月的第几天
     * @param date
     * @return
     * @throws ParseException
     */
    public static Integer getMonthDayNum(String date) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(date);
        Date d2=getMonthStartTime(d);
        Calendar currCal=Calendar.getInstance();
        currCal.setTime(d);
        Calendar currCal2=Calendar.getInstance();
        currCal2.setTime(d2);
        long a = currCal.getTimeInMillis()-currCal2.getTimeInMillis();
        a=a/(1000*3600*24);
        return (int)a;
    }
    public static Integer getYearDayNum(String date) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(date);
        Date d2=getYearStartTime(d);
        Calendar currCal=Calendar.getInstance();
        currCal.setTime(d);
        Calendar currCal2=Calendar.getInstance();
        currCal2.setTime(d2);
        long a = currCal.getTimeInMillis()-currCal2.getTimeInMillis();
        a=a/(1000*3600*24);
        return (int)a;
    }

    /**
     * 获取当年1月1号0:00
     * @param year
     * @return
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 格式化String格式的时间
     * @param str
     * @param datefomart
     * @param strFomart
     * @return
     * @throws ParseException
     */
    public static String dateFomart(String str,String datefomart,String strFomart) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat(datefomart);
        SimpleDateFormat sdf2=new SimpleDateFormat(strFomart);
        Date d = sdf.parse(str);
        str = sdf2.format(d);

        return str;
    }

    /**
     *  字符串转时间
     * @param str
     * @param datefomart
     * @return
     * @throws ParseException
     */
    public static Date strToDate(String str,String datefomart) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat(datefomart);
        Date d = sdf.parse(str);
        return d;
    }

    /**
     * 时间转字符串
     * @param str
     * @param datefomart
     * @return
     * @throws ParseException
     */
    public static String dateToStr(Date str,String datefomart) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat(datefomart);
        String d = sdf.format(str);
        return d;
    }

    /**
     *
     * @param time
     * @param hour
     * @param format
     * @return
     */
    public static String setHour(String time,String hour,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();

        try {
            Date d = sdf.parse(time);
            calendar = Calendar.getInstance();
            calendar.setTime(d);
            calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hour));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(calendar.getTime());
    }

    /**
     * 4个时间点： 6 9 16 24
     * 如果小于6 。则为24点。四个时间点都为昨天时间。那么，顺序为6,9,16,24
     * if <9 & >=6 则为6点。其他三个时间点都为昨天。顺序：9,16,24,6
     * if <16 >=9 则为9点 则16,24为昨天时间。 顺序 16,24,6,9
     * if <24 >=16 ~16点~ 24为昨天时间 顺序 24,6,9,16
     * @param time
     * @return
     */
    public static List<String> getFourTimes(String time){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
        Calendar calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        List<String> list = new ArrayList<>();
        try {
            Date d = sdf.parse(time);
            calendar.setTime(d);
            calendar.set(Calendar.MINUTE,0);
            calendar2.setTime(d);
            calendar2.set(Calendar.MINUTE,0);
            calendar2.add(Calendar.DAY_OF_MONTH,-1);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if(hour<6){
                calendar2.set(Calendar.HOUR_OF_DAY,6);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar2.set(Calendar.HOUR_OF_DAY,9);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar2.set(Calendar.HOUR_OF_DAY,16);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar2.set(Calendar.HOUR_OF_DAY,24);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
            }else if(hour>=6 && hour<9){
                calendar2.set(Calendar.HOUR_OF_DAY,9);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar2.set(Calendar.HOUR_OF_DAY,16);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar2.set(Calendar.HOUR_OF_DAY,24);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar.set(Calendar.HOUR_OF_DAY,6);
                list.add(dateToStr(calendar.getTime(),"yyyyMMddHHmm"));
            }else if(hour>=9 && hour<16){
                calendar2.set(Calendar.HOUR_OF_DAY,16);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar2.set(Calendar.HOUR_OF_DAY,24);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar.set(Calendar.HOUR_OF_DAY,6);
                list.add(dateToStr(calendar.getTime(),"yyyyMMddHHmm"));
                calendar.set(Calendar.HOUR_OF_DAY,9);
                list.add(dateToStr(calendar.getTime(),"yyyyMMddHHmm"));
            }else if(hour>=16 && hour<24){
                calendar2.set(Calendar.HOUR_OF_DAY,24);
                list.add(dateToStr(calendar2.getTime(),"yyyyMMddHHmm"));
                calendar.set(Calendar.HOUR_OF_DAY,6);
                list.add(dateToStr(calendar.getTime(),"yyyyMMddHHmm"));
                calendar.set(Calendar.HOUR_OF_DAY,9);
                list.add(dateToStr(calendar.getTime(),"yyyyMMddHHmm"));
                calendar.set(Calendar.HOUR_OF_DAY,16);
                list.add(dateToStr(calendar.getTime(),"yyyyMMddHHmm"));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 计算得到MongoDB存储的日期，（默认情况下mongo中存储的是标准的时间，中国时间是东八区，存在mongo中少8小时，所以增加8小时）
     * http://www.iteye.com/problems/88507
     *
     * @author: Gao Peng
     * @date: 2016年5月4日 上午9:26:23
     * @param: @param
     *             date
     * @param: @return
     * @return: Date
     */
    public static Date getMongoDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, 8);
        return ca.getTime();
    }
}
